<?php

class CalendarForm extends PostBackControl {

	var $CalendarManager;
	var $CalendarData;
	var $CalendarSelect;
	var $ColorSelect;
	var $CalendarRoles;
	var $Calendar;
	var $ColorScheme;
	var $Color;
	var $CustomFieldTypeSelect;

	function CalendarForm(&$Context) {
		$this->Name = 'CalendarForm';
		$this->ValidActions = array('Calendars', 'Calendar', 'ProcessCalendar', 'CalendarRemove', 'ProcessCalendarRemove');
		$this->ColorSchemeFile = './extensions/Vcal/theme/ColorScheme.php';
		$this->ColorScheme = $this->GetColorScheme();
		$this->Color = array();
		$this->Constructor($Context);
		if ($this->IsPostBack) {
			$RedirectUrl = '';
			$this->Context->PageTitle = $this->Context->GetDefinition('VcalCalendarManagement');

			// Add the javascript to the head for sorting Calendars
			if ($this->PostBackAction == "Calendars") {
				global $Head;
				$Head->AddScript('js/prototype.js');
				$Head->AddScript('js/scriptaculous.js');
			}

			$CalendarID = ForceIncomingInt('CalendarID', 0);
			$ReplacementCalendarID = ForceIncomingInt('ReplacementCalendarID', 0);
			$this->CalendarManager = $this->Context->ObjectFactory->NewContextObject($this->Context, 'CalendarManager');

			if ($this->PostBackAction == 'ProcessCalendar' && $this->IsValidFormPostBack()) {
				$this->Calendar = $this->Context->ObjectFactory->NewContextObject($this->Context, 'Calendars');
				$this->Calendar->GetPropertiesFromForm($this->Context);
				$Action = ($this->Calendar->CalendarID == 0) ? "SavedNew" : "Saved";
				if (($this->Calendar->CalendarID > 0 && $this->Context->Session->User->Permission('PERMISSION_VCAL_EDIT_CALENDAR'))
					|| ($this->Calendar->CalendarID == 0 && $this->Context->Session->User->Permission('PERMISSION_VCAL_ADD_CALENDAR'))) {
					if ($this->CalendarManager->SaveCalendar($this->Calendar)) {
						$this->CallDelegate('PostSaveCalendar');
						$RedirectUrl = GetUrl(
							$this->Context->Configuration, $this->Context->SelfUrl, '', '', '', '',
							'PostBackAction=Calendars&Action='.$Action);
					} 
				} else {
					$this->IsPostBack = 0;
				}
			} elseif ($this->PostBackAction == 'ProcessCalendarRemove' && $this->IsValidFormPostBack()) {
				if ($this->Context->Session->User->Permission('PERMISSION_VCAL_DELETE_CALENDAR')) {
					if ($this->CalendarManager->RemoveCalendar($CalendarID, $ReplacementCalendarID)) {
						$RedirectUrl = GetUrl(
							$this->Context->Configuration, $this->Context->SelfUrl, '', '', '', '',
							'PostBackAction=Calendars&Action=Removed');
					}
				} else {
					$this->IsPostBack = 0;
				}
			}

			if (in_array($this->PostBackAction, array( 'Calendars', 'Calendar', 'ProcessCalendar', 'CalendarRemove', 'ProcessCalendarRemove'))) {
				$this->CalendarData = $this->CalendarManager->GetCalendars(0, 0, 0);
			}
			if (in_array($this->PostBackAction, array('CalendarRemove', 'ProcessCalendarRemove'))) {
				$this->CalendarSelect = $this->Context->ObjectFactory->NewObject($this->Context, 'Select');
				$this->CalendarSelect->Name = 'CalendarSelect';
				$this->CalendarSelect->CssClass = 'SmallInput';
				$this->CalendarSelect->AddOptionsFromDataSet($this->Context->Database, $this->CalendarData, 'CalendarID', 'Title');
			}
			if (in_array($this->PostBackAction, array('Calendar', 'ProcessCalendar'))) {
				//Add the color select field
				$this->ColorSelect = $this->Context->ObjectFactory->NewObject($this->Context, 'Select');
				$this->ColorSelect->Name = 'ColorSelect';
				$this->ColorSelect->CssClass = 'SmallInput form-select';
				$this->ColorSelect->Attributes = 'id="edit-scheme"';
				while (list($key, $val) = each($this->ColorScheme)) {
					$this->ColorSelect->AddOption(implode(",",$val), $key);
				}
				
				if ($CalendarID > 0) {
					$this->Calendar = $this->CalendarManager->GetCalendarById($CalendarID);
										
					// If editing an existing calendar add calendar select field
					$this->CalendarSelect = $this->Context->ObjectFactory->NewObject($this->Context, 'Select');
					$this->CalendarSelect->Name = 'CalendarSelect';
					$this->CalendarSelect->CssClass = 'SmallInput';
					$this->CalendarSelect->AddOptionsFromDataSet($this->Context->Database, $this->CalendarData, 'CalendarID', 'Title');

					foreach ($this->ColorScheme as $key => $value) {
						$temparray[] = $value;
					}
					if (in_array($this->Calendar->ColorArr, $temparray)) {
						$this->ColorSelect->SelectedValue = implode(',',$this->Calendar->ColorArr);
					}
					else {
						$this->ColorSelect->SelectedValue = $this->ColorScheme["Custom"];
					}

				} else {
					// If creating a new calendar
					$this->Calendar = $this->Context->ObjectFactory->NewObject($this->Context, 'Calendars');
					$this->ColorSelect->SelectedValue = $this->ColorScheme["(Default)"];
					$this->Calendar->ColorArr = $this->ColorSelect->SelectedValue;
				}
				
				//Add calendar roles checkboxes
				$this->CalendarRoles = $this->Context->ObjectFactory->NewObject($this->Context, 'Checkbox');
				$this->CalendarRoles->Name = 'CalendarRoleBlock[]';
				$this->CalendarRoles->CssClass = 'CheckBox';
				$CalendarRoleData = $this->CalendarManager->GetCalendarRoleBlocks($CalendarID);
				$this->CalendarRoles->AddOptionsFromDataSet($this->Context->Database, $CalendarRoleData,'RoleID','Name','Blocked',1);
				
				//Add Calendar Custom field type select field
				$this->CustomFieldTypeSelect = $this->Context->ObjectFactory->NewObject($this->Context, 'Select');
				$this->CustomFieldTypeSelect->Name = 'CustomFieldType[]';
				$this->CustomFieldTypeSelect->CssClass = 'SmallInput';
				$this->CustomFieldTypeSelect->AddOptionsFromAssociativeArray(array("-1" => "-- Select type of field --" , "checkbox" => "Checkbox", "input" => "Input field", "textarea" => "Textarea"), ''); 				
			}

			if (in_array($this->PostBackAction, array('ProcessCalendar', 'ProcessCalendarRemove'))) {
				// Show the form again with errors
				$this->PostBackAction = str_replace('Process', '', $this->PostBackAction);
			}

			if ($RedirectUrl) {
				//@todo: should the process die here?
				Redirect($RedirectUrl, '302', '', 0);
			}
		}
		$this->CallDelegate('Constructor');
	}
	/**
	* Read the ColorSchemeFile
	*/
	function GetColorScheme()
	{
		if(file_exists($this->ColorSchemeFile)) return include($this->ColorSchemeFile);
		else return 0;
	}
	function Render() {
		if ($this->IsPostBack) {
			$this->CallDelegate('PreRender');
			$this->PostBackParams->Clear();
			$CalendarID = ForceIncomingInt('CalendarID', 0);

			if ($this->PostBackAction == 'Calendar') {
				$this->PostBackParams->Set('PostBackAction', 'ProcessCalendar');
				$this->CallDelegate('PreEditRender');
				include THEME_ROOT . 'Theme.CalendarEdit.php';
				$this->CallDelegate('PostEditRender');

			} elseif ($this->PostBackAction == 'CalendarRemove') {
				$this->PostBackParams->Set('PostBackAction', 'ProcessCalendarRemove');
				$this->CalendarSelect->Attributes = "onchange=\"document.location='".GetUrl($this->Context->Configuration, $this->Context->SelfUrl, '', '', '', '', 'PostBackAction=CalendarRemove')."&amp;CalendarID='+this.options[this.selectedIndex].value;\"";
				$this->CalendarSelect->SelectedValue = $CalendarID;
				$this->CallDelegate('PreRemoveRender');
				include THEME_ROOT . 'Theme.CalendarRemove.php';
				$this->CallDelegate('PostRemoveRender');

			} else {
				$this->PostBackParams->Set('PostBackAction', 'ProcessCalendars');
				$this->CallDelegate('PreListRender');
				include THEME_ROOT . 'Theme.CalendarList.php';
				$this->CallDelegate('PostListRender');

			}
			$this->CallDelegate('PostRender');
		}
	}
}
?>